package com.foreveredu.erlms.web.struts.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.erlms.domain.Enroll;
import com.foreveredu.erlms.domain.Examinee;
import com.foreveredu.erlms.domain.PageModel;
import com.foreveredu.erlms.service.IExamService;
import com.foreveredu.erlms.web.struts.actionform.EnrollActionForm;

/*
 * @author liwei 2012-04-26 15:42:43
 * EnrollPaidAction未交费学生列表
 */
public class EnrollPaidAction extends AbstractBaseDispatchAction {

	private IExamService examService = null;

	public IExamService getExamService() {
		return examService;
	}

	public void setExamService(IExamService examService) {
		this.examService = examService;
	}

	// 后台报名信息列表(已交费学生列表)
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		//获取当前考次
		int examOrder = examService.getExamOrderOfNow();
		
		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if (offset < 0) {
			
			return mapping.findForward("enrollList");
		}
		PageModel pm = examService.findEnrollListByPaid(offset,examOrder);
		
		request.setAttribute("pm", pm);
		return mapping.findForward("enrollList");
	}

	// 改变报名信息交费状态
	public ActionForward updateEnrollPaid(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		EnrollActionForm enrollForm = (EnrollActionForm) form;
		Enroll enroll = examService.findEnrollById(enrollForm.getId());
		
		if (enroll.getIsPaid() == 0) {
			// 当考生交费后，生成该考生的报名号，并修改该考生的相片的文件名为报名号
			String enrollNumber = examService.getMaxENRecord(enroll.getRank().getCode());
			Examinee examinee = enroll.getExaminee();
			examinee.setEnrollNumber(enrollNumber);
			examService.updateExaminee(examinee);

			String photo = request
					.getSession()
					.getServletContext()
					.getRealPath(
							"/photo/" + enroll.getIcon());
			File file = new File(photo);
			log.info(photo);
			
			String newPhoto = request.getSession().getServletContext()
					.getRealPath("/photo/" + enrollNumber + ".jpg");
			File newFile = new File(newPhoto);
			log.info(newPhoto);
			
			if (file != null) {
				file.renameTo(newFile);
			}
			
			enroll.setIcon(enrollNumber + ".jpg");
			
			//将当前日期储存到enroll中
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String now = sdf.format(new Date());
			enroll.setPaidTime(now);
			enroll.setIsPaid(1);
			
			examService.updateEnroll(enroll);
			
			return mapping.findForward("toEnrollList");
		}
		return mapping.findForward("toEnrollList");
	}

}
